#!/usr/bin/perl -w

###############################################################################
use strict; # Always working in strict mode with warnings on

###############################################################################
use File::Basename;
use File::Copy;
use Cwd;
use XML::Simple; # install it from cpan 
use Data::Dumper;

###############################################################################
# Subroutine declarations
###############################################################################
sub show_help($);
sub get_projects_names();
sub create($);
sub setup($);
sub replace_in_file($$$);
sub get_argument($);
sub read_config();

###############################################################################
# Global variables
###############################################################################
# Messaging and this script name/location/version related.
my $PROGNAME = basename($0);
my $PROGVER = "0.1";

my $config_file = "config.xml";
my $config; 	# contents input datas which are loaded from the xml

my $builder_path;
my $templates_path;

my $build_system;
my $test_project = "src/test_project";

###############################################################################
# Main Block 
###############################################################################
show_help(0) if (exists $ARGV[0] and $ARGV[0] =~ /^\-h(elp)?$/);

read_config();
$builder_path   = $config->{templates_path};
$templates_path = $config->{builder_path};
my $projects = get_argument("name");
print $projects, "\n";
#my @projects = get_projects_names();
#create(\@projects);
#setup(\@projects);

###############################################################################
# Subroutine definitions
###############################################################################

#=============================================================================
# reads config_file
sub read_config()
{
	# getting all inputs from the config_file
	my $xml_config = XML::Simple->new();
	$config = $xml_config->XMLin($config_file);
# DEBUG
	#print Dumper(%$config);
# END
}

#=============================================================================
# builds projects corresponding to the data list
sub get_argument($)
{
	my ($p) = @_;
	if (join(' ', @ARGV) =~ /(.*)?-$p[ |=|"]([\w|\/]*)[ |\$|"]*/) {
		return $2;
	}
	return "";
}

#===============================================================================
# setupt the variables values in the makefiles
sub replace_in_file($$$)
{
	my ($file, $src, $dest) = @_;
	open (FILE, "<$file");
	my @new_content;
	while (<FILE>) {
		s/$src/$dest/;
		push(@new_content, $_);
	}
	close(FILE);
	open (FILE, ">$file");
	foreach (@new_content) {
		print FILE $_; 
	}
	close(FILE);
}

#===============================================================================
# setupt the variables values in the makefiles
sub setup($)
{
	my ($projects) = @_;
	foreach (@$projects) {
		my $project = $_;
		replace_in_file("$project/makefile", 
						"\@projects@", 
						"projects := \\\n\t$test_project");
		replace_in_file("$project/$test_project/makefile", 
						"\@build_system@", 
						"build_system := $build_system");
		replace_in_file("$project/$test_project/unit_tests/makefile", 
						"\@build_system@", 
						"build_system := $build_system");
	}
}

#===============================================================================
# Returns the names of the projects, whih are either the arguments of the 
# script, or the result of dialog with the user
sub create($)
{
	my ($projects) = @_;
	foreach (@$projects) {
		mkdir $_;
		copy("$templates_path$/top_makefile", "$_/makefile");
		mkdir "$_/src";
		mkdir "$_/$test_project";
		copy("$templates_path/project_makefile", "$_/$test_project/makefile");
		mkdir "$_/$test_project/unit_tests";
		copy("$templates_path/unit_test_makefile", "$_/$test_project/unit_tests/makefile");
		copy("$templates_path/main.cpp", "$_/$test_project/main.cpp");
		copy("$templates_path/main.cpp", "$_/$test_project/unit_tests/main.cpp");
	}
}

#===============================================================================
# Returns the names of the projects, whih are either the arguments of the 
# script, or the result of dialog with the user
sub get_projects_names()
{
	my @projects;
	if ($#ARGV < 0) {
		print "Project name: ";
		@projects = split(" ", <STDIN>);
	} else {
		@projects = @ARGV;
	}
	return @projects;
}

#===============================================================================
# Show help message and exit with given exit code (0 is assumed success status)
# In: exit code
sub show_help($)
{
	my $status = shift;
	print qq!$PROGNAME

	NAME
		$PROGNAME - Description  

		SYNOPSYS
			$PROGNAME [option] makefiles_list

			DESCRIPTION

			OPTIONS
				-h[elp]            Print this help screen and exit


				ENVIRONMENT

				RESTRICTIONS
					Tested with GNU C/C++ compilers only.

					SEE ALSO
						gcc(1) g++(1)

						AUTHOR
							Tigran Hovhannisyan 

							REVISION HISTORY
							!;
							exit($status);
} # show_help()

################################################################################
#                          E N D  O F  S C R I P T
################################################################################

